<?php
class City_model extends Base_Model
{

    public function __construct()
    {
        $this->load->database();
    }

    public function getcitytype($cityId=0)
    {
        $this->db->select('c.id as cityId, c.cityName,c.countryName,c.continentName, t.id as typeId,t.name as typeName');
        $this->db->from('mod_scenic_type as t');
        $this->db->join('mod_city as c', 't.cityId = c.id', 'left');
		if(!empty($cityId))
		{$this->db->where('c.id',$cityId);}
		$query = $this->db->get()->result_array();
        $citys =array();
        if(!empty($query))
        {
            foreach ($query as $q)
            {//去除自定义景点
              if($q['typeId']!=2){
                $type = array();
                $type['typeId'] = $q['typeId'];
                $type['typeName'] = $q['typeName'];
                $citys[$q['cityId']]['name'] = $this->getname($q['cityName'],"CN");
                $citys[$q['cityId']]['id'] = $q['cityId'];
                $citys[$q['cityId']]['tags'][] = $type;
              }
            }
		}
		foreach($citys as &$v){
			$v['tags'][]=array('typeId'=>'0','typeName'=>'不限');
			$v['tags']=array_reverse($v['tags']);
		}
        return array_values($citys);
    }

    public function getcity()
    {
        $this->db->from('mod_city ');
        $query = $this->db->get()->result_array();
        $citys =array();
        if(!empty($query))
        {
            foreach ($query as $q)
            {

                $q['cityName'] = $this->getname($q['cityName'],"CN") ;
                $q['continentName']  = $this->getname($q['continentName'],"CN");
				$q['countryName']  = $this->getname($q['countryName'],"CN");
				//city部分
                $citys[$q['countryName']][$q['id']]['name'] = $q['cityName'];
				$citys[$q['countryName']][$q['id']]['id'] = $q['id'];
				//country部分
				$cou[$q['continentName']][$q['countryName']]['country'] = $q['countryName'];
				$cou[$q['continentName']][$q['countryName']]['content']=array_values($citys[$q['countryName']]);
			    //合并最终结果
				$ret[$q['continentName']]['area'] = $q['continentName'];
				$ret[$q['continentName']]['content']=array_values($cou[$q['continentName']]);
            }
        }
        return array_values($ret);
    }

    private function getname($name,$langue)
    {
    	$name=trim($name);
    	$name=rtrim($name,"|");
        $str = explode('|',$name);
        $langue=strtoupper($langue);
        switch ($langue){
        	case "CN":
        		$name=$str[0];
        		break;
        	case "EN":
        		$name=$str[1];
        		break;
        	default:
        		$name=$str[0];
        }
        return $name;
    }

}
